import { useState } from "react";
import { PlusOutlined } from "@ant-design/icons";
import { Button } from "antd";
import EditModal from "./edit-modal";

const createPromise = (data: any) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(data);
    }, 1000);
  });
};

export default function CreateButton({ refresh }: { refresh: () => void }) {
  const [open, setOpen] = useState(false);

  const onCreate = (values: any) => {
    return createPromise(values);
  };

  const handleOpen = () => setOpen(true);
  const handleClose = () => {
    setOpen(false);
    refresh();
  };

  return (
    <>
      <Button size="middle" type="primary" icon={<PlusOutlined />} onClick={handleOpen}>
        添加
      </Button>
      <EditModal open={open} onCreate={onCreate} onCancel={handleClose} />
    </>
  );
}
